# TODO Reinstate the autocreation that Chris Brown wrote

class Seiso::ImportMaster
  # Imports service instance ports into Seiso. This importer handles:
  # - orphan removal
  # - POSTing new ports
  # - PUTting existing ports
  #
  # Author:: Willie Wheeler
  # Copyright:: Copyright (c) 2014-2016 Expedia, Inc.
  # License:: Apache 2.0
  class Importers::ServiceInstancePortImporter < Importers::BaseImporter

    def initialize(api, rest_util)
      self.api = api
      self.rest_util = rest_util
      self.mapper = Mappers::ServiceInstancePortMapper.new
      self.repo_resource = api.serviceInstancePorts.get

      @search_resource = repo_resource.search.get
    end

    # Can't make these protected, because that prevents BaseImporter from seeing them.
#    protected

    # BaseImporter callback
    def to_search_params(doc_sip, context)
      {
        'si' => context['serviceInstance'].key,
        'number' => doc_sip['number']
      }
    end

    # BaseImporter callback
    def find_resource(search_params)
      si = search_params['si']
      number = search_params['number']
      @search_resource.findByServiceInstanceKeyAndNumber(si: si, number: number).get
    end
  end
end
